﻿using System;
using System.Collections.Generic;
using System.Data;
using Dapper;
using BCEntity.TMS.Driver.DriverExamine;
using BCCommon;
using System.Threading.Tasks;

namespace BCData.TMS.Driver.DriverExamine
{
    public class DriverExaminePaperData : IDriverExaminePaperData
    {
        private const string QUERY_SQL = "SELECT PaperId,CorrectAmount,ErrorAmount,CreateTime,EndTime,AppliedTo FROM  DriverExaminePapers ";

        private readonly IDatabaseContext databaseContext;
        public DriverExaminePaperData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        /// <summary>
        /// 新增考试试卷
        /// </summary>
        /// <param name="entity">考试试卷信息</param>
        /// <returns></returns>
        public async Task<DriverExaminePaperEntity> InsertAsync(DriverExaminePaperEntity entity)
        {
            string sql = string.Format(@"INSERT INTO DriverExaminePapers(CorrectAmount,ErrorAmount,CreateTime,EndTime,AppliedTo) 
                                         VALUES(?CorrectAmount,?ErrorAmount,?CreateTime,?EndTime,?AppliedTo); 
                                        {0} Where PaperId = LAST_INSERT_ID();", QUERY_SQL);
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?CorrectAmount", entity.CorrectAmount, DbType.Int32);
            parameter.Add("?ErrorAmount", entity.ErrorAmount, DbType.Int32);
            parameter.Add("?CreateTime", DateTime.Now, DbType.DateTime);
            parameter.Add("?EndTime", DateTime.Now, DbType.DateTime);
            parameter.Add("?AppliedTo", entity.AppliedTo.ToString(), DbType.AnsiString);
            return await databaseContext.Master.QueryFirstOrDefaultAsync<DriverExaminePaperEntity>(sql, parameter);
        }

        /// <summary>
        /// 检查试卷信息是否存在
        /// </summary>
        /// <param name="paperId">试卷Id</param>
        /// <returns></returns>
        public async Task<bool> CheckExistsByIdAsync(long paperId)
        {
            string sql = @"SELECT 1 FROM DriverExaminePapers WHERE PaperId=?PaperId LIMIT 1";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?PaperId", paperId, DbType.Int64);
            var result = await databaseContext.Master.ExecuteScalarAsync(sql, parameter);
            return result != null;
        }

        /// <summary>
        /// 删除考试试卷
        /// </summary>
        /// <param name="paperId">试卷Id</param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int paperId)
        {
            string sql = @"DELETE FROM DriverExaminePapers WHERE PaperId=?PaperId";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?PaperId", paperId, DbType.Int64);
            var i = await databaseContext.Master.ExecuteAsync(sql, parameter);
            return i > 0;
        }

        /// <summary>
        /// 获取考试试卷
        /// </summary>
        /// <param name="paperId">试卷Id</param>
        /// <returns></returns>
        public async Task<DriverExaminePaperEntity> GetExaminePaperByIdAsync(int paperId)
        {
            string sql = string.Format(@"{0} WHERE PaperId=?PaperId", QUERY_SQL);
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?PaperId", paperId, DbType.Int64);
            return await databaseContext.Master.QueryFirstOrDefaultAsync<DriverExaminePaperEntity>(sql, parameter);
        }

        /// <summary>
        /// 分页获取考试试卷信息
        /// </summary>
        /// <param name="pageIndex">分页页码</param>
        /// <param name="pageSize">分页页数</param>
        /// <returns></returns>
        public async Task<(IEnumerable<DriverExaminePaperEntity>, int)> GetDriverExaminePapersAsync(int pageIndex, int pageSize)
        {
            pageIndex = pageIndex == 0 ? 1 : pageIndex;
            int startIndex = (pageIndex - 1) * pageSize;
            string sql = string.Format(@"{0}  LIMIT {1} , {2}; SELECT COUNT(1) FROM DriverExaminePapers;", QUERY_SQL, startIndex, pageSize);
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?startIndex", startIndex, DbType.Int32);
            parameter.Add("?pageSize", pageSize, DbType.Int32);

            var result = await databaseContext.QueryMultipleAsync<DriverExaminePaperEntity, int>(sql, parameter);

            return result;
        }

        /// <summary>
        /// 修改考试试卷
        /// </summary>
        /// <param name="entity">试卷信息</param>
        /// <returns></returns>
        public async Task<DriverExaminePaperEntity> UpdateAsync(DriverExaminePaperEntity entity)
        {
            string sql = string.Format("UPDATE DriverLearnDatumAnswer SET CorrectAmount=?CorrectAmount,ErrorAmount=?ErrorAmount,EndTime=?EndTime WHERE PaperId=?PaperId;{0} WHERE PaperId=?PaperId;", QUERY_SQL);
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?PaperId", entity.PaperId, DbType.Int64);
            parameter.Add("?CorrectAmount", entity.CorrectAmount, DbType.Int32);
            parameter.Add("?ErrorAmount", entity.ErrorAmount, DbType.Int32);
            parameter.Add("?EndTime", entity.EndTime, DbType.DateTime);
            return await databaseContext.Master.QueryFirstOrDefaultAsync<DriverExaminePaperEntity>(sql, parameter);
        }

        /// <summary>
        /// 检查试卷信息是否存在
        /// </summary>
        /// <param name="correctAmount">正确答案</param>
        /// <param name="errorAmount">错误答案</param>
        /// <returns></returns>
        public async Task<DriverExaminePaperEntity> CheckExistsByAmountsAsync(int correctAmount, int errorAmount)
        {
            string sql = @"SELECT PaperId,CorrectAmount,ErrorAmount,CreateTime,EndTime FROM  DriverExaminePapers WHERE CorrectAmount=?CorrectAmount AND  ErrorAmount=?ErrorAmount LIMIT 1";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?CorrectAmount", correctAmount, DbType.Int32);
            parameters.Add("?ErrorAmount", errorAmount, DbType.Int32);
            return await databaseContext.Master.QueryFirstOrDefaultAsync<DriverExaminePaperEntity>(sql, parameters);
        }

        /// <summary>
        /// 更新答题完成时间
        /// </summary>
        /// <param name="paperId">试卷Id</param>
        /// <returns></returns>
        public async Task<DriverExaminePaperEntity> UpdateEndTimeAsync(long paperId)
        {
            string sql = string.Format("UPDATE DriverExaminePapers SET EndTime=?EndTime WHERE PaperId=?PaperId;{0} WHERE PaperId=?PaperId;", QUERY_SQL);
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?PaperId", paperId, DbType.Int64);
            parameter.Add("?EndTime", DateTime.Now, DbType.DateTime);
            return await databaseContext.Master.QueryFirstOrDefaultAsync<DriverExaminePaperEntity>(sql, parameter);
        }
    }
}
